import { Node } from "cc";
import { IVector2, Maths, Move, Sets, Vector2, Vector3, _logic, ccclass } from "../../../main/script/Main";
import { EEntityType, ILogicNuoData } from "../../../main/script/module/define/LogicDefine";
import { Item } from "../Item";
import { Entity } from "./Entity";
import { TipEntity } from "./TipEntity";

const v2T = new Vector2()
const v3T = new Vector3()

@ccclass("CubeNuoEntity")
export class CubeNuoEntity extends Entity {

    public entityType = EEntityType.Cube
    public isPut = true
    public isUpdateStateMackine = true
    public isDestroy = false
    public isMoving() { return false }

    private cADragTip: Node = null!

    public data: ILogicNuoData = null!
    private _move = new Move()

    public animComplete: () => void = null!

    public _isTouchMovePos = false
    public touchMovePosTarget = new Vector2()
    protected _moveRatio = 0.5

    private isResetAnim = false
    protected _curAnimCount = 0
    protected _toAnimCount = 0
    private _tipAnimEntity: TipEntity = null!

    public _isLerpAnim = false
    public _lerpPos = new Vector2()
    private _lastChildIndex = -1

    public _isTouchMovePos2 = false

    public get item() { return this.getCacheComponent(Item) }

    protected onStateEnterLoad(): void {
        super.onStateEnterLoad()
        this.setScaleNum(_logic.scaleRatio, false)

        this._move.init(this.node, false, 66 * 60)


        let _pos: IVector2 = !this.animComplete ? this.data.rect.center : _logic.nuo.data.datas[_logic.animIndex].rect.center
        this.setPositionXY(_pos, false)
    }

    protected onStateEnterRun(): void {
        super.onStateEnterRun()
        if (this.animComplete) {
            this._move.setRunData(1, runDData => {
                Vector2.set(runDData.target, this.data.rect.center)
                runDData.speed = 3600
            })
            this._move.run(this.animComplete)
        }

        this.item.setIconState(false)

        this.setDragTipState(false)
    }

    public startAnim() {
        this.item.setConfigFrame(this.data.configId)
        // this.item.scaleAnim(.3, null!, true, this.data.index.y % 2 == 0 ? "x" : "y")
        this.item.scaleAnim(.3, null!, true, "x")
    }

    public touchMoveStartBefore() {
        if (this._lastChildIndex == -1) {
            this._lastChildIndex = this.node.getSiblingIndex()
        }
    }

    public touchMoveStart() {
        this._isTouchMovePos = true
        this._isTouchMovePos2 = true

        // this.node.setSiblingIndex(this.node.parent.children.length - 1)
    }

    public touchMoveing(space: Vector2) {
        let _center = this.data.rect.center
        this._isTouchMovePos = true
        Vector2.add(this.touchMovePosTarget, _center, space)
        this._moveRatio = 0.5
        this._isTouchMovePos2 = true
    }

    public touchMoveStop() {
        this._isTouchMovePos = true
        this.touchMovePosTarget.set(this.data.rect.center)
        this._moveRatio = 0.2
        this._isTouchMovePos2 = false

        // if (this._lastChildIndex != -1)
        //     this.node.setSiblingIndex(this._lastChildIndex)
        // this._lastChildIndex = -1
    }

    public touchMoveRemove(pos: IVector2) {

        Vector2.set(this.touchMovePosTarget, pos)
        this._moveRatio = 0.2
        this._isTouchMovePos = true
    }


    protected onStateUpdateRun(): void {
        super.onStateUpdateRun()
        this._move.onUpdate()

        if (this.isResetAnim) {
            this._curAnimCount++
            if (this._curAnimCount >= this._toAnimCount) {
                this._toAnimCount += Maths.minToMax(5, 20)
                this.item.setConfigFrame(Sets.random(_logic.nuo._resetConfigs))
            }
        }

        if (this._isTouchMovePos) {
            v3T.set(this.touchMovePosTarget.x, this.touchMovePosTarget.y, 0)
            if (this.lerpTarget(v3T, this._moveRatio, undefined, false))
                this._isTouchMovePos = false

        }
    }

    public setDragTipState(_value: boolean) {
        this.cADragTip.active = _value
    }

    public getZIndex() {
        let _value = this.data.index.y * 1000

        if (this.data.isRemove)
            _value += 50

        let nuo = this.scene.nuo
        if (nuo.touchMoveDir.x == 0) {
            if (this._isTouchMovePos2) {

                _value -= _logic.size.y * 1000
                // _value -= (nuo.moveRemoveDatas.length) * 1000
            }
        }
        else {

        }
        return _value
    }

    protected onStateEnterExit(arg?: string): void {
        this._move.clear()
        this.tipAnimStop()
        this._isLerpAnim = false
        this._lastChildIndex = -1
        this._isTouchMovePos = false
        this._isTouchMovePos2 = false
        this.setDragTipState(false)
        super.onStateEnterExit()
    }


    public playResetAnim() {
        this._curAnimCount = 0
        this._toAnimCount = 0
        this.isResetAnim = true
    }

    public stopResetAnim() {
        this._curAnimCount = 0
        this._toAnimCount = 0
        this.isResetAnim = false

        this.item.setConfigFrame(this.data.configId)
    }

    public remove(isLeft: boolean, scaleAnimX: boolean, complete?: () => void) {
        // let entity = this.entityMgr.createCubeAnim()
        // entity.startAnim(
        //     this.data.configId,
        //     this.node.position,
        //     isLeft,
        //     complete
        // )
        this.data.isRemove = true
        this.item.scaleAnim(.3, null!, false, scaleAnimX ? "x" : "y")
    }


    public tipAnim() {
        this._tipAnimEntity = this.scene.entityMgr.createTipNuo(this.data)
    }

    public tipAnimStop() {
        if (this._tipAnimEntity) {
            this.scene.entityMgr.remove(this._tipAnimEntity)
            this._tipAnimEntity = null!
        }
    }


    public getUIPosition(out: IVector2, center: IVector2 = this.data.rect.center) {
        Vector2.set(out, center)
    }


}